<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId">
              <j-select-area placeholder="请选择区域" v-model="model.areaId" multi />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="受益农户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beneficiaryFarmers">
              <a-input placeholder="请输入" v-model="model.beneficiaryFarmers" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终端名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="terminalName">
              <a-input v-model="model.terminalName" placeholder="请输入终端名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终端编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="terminalNo">
              <a-input v-model="model.terminalId" placeholder="请输入终端编号" disabled></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manager">-->
<!--              <a-input v-model="model.manager" placeholder="请输入负责人"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="managerTel">-->
<!--              <a-input v-model="model.managerTel" placeholder="请输入联系电话"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input-number v-model="model.longitude" placeholder="请输入经度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input-number v-model="model.latitude" placeholder="请输入纬度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="占地面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="floorSpace">
              <a-input v-model="model.floorSpace" placeholder="请输入占地面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="material">
              <j-dict-select-tag v-model="model.material" placeholder="请选择" dictCode="nw_terminal_material" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="厂家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factory">
              <a-input v-model="model.factory" placeholder="请输入厂家"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="厂家电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factoryTel">
              <a-input v-model="model.factoryTel" placeholder="请输入厂家电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <a-input v-model="model.brand" placeholder="请输入品牌"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <a-input v-model="model.position" placeholder="请输入位置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="使用寿命" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceLife">
              <a-input v-model="model.serviceLife" placeholder="请输入使用寿命" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="日处理能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dailyProcessing">
              <!-- <a-input v-model="model.dailyProcessing" placeholder="请输入日处理能力"></a-input> -->
              <j-dict-select-tag v-model="model.dailyProcessing" placeholder="请选择" dictCode="nw_terminal_dealable" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="排放标准" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="emissionStandards">
              <j-dict-select-tag
                v-model="model.emissionStandards"
                placeholder="请选择"
                dictCode="nw_terminal_emission_standards"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="安装时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="installTime">
              <j-date
                placeholder="请选择安装时间"
                v-model="model.installTime"
                :show-time="false"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <!--  -->
          <a-col :span="12">
            <a-form-model-item label="处理工艺" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceLife">
              <a-input v-model="model.treatmentTechnology" placeholder="请输入处理工艺" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终端组成设施" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="facilityType">
              <div v-for="(i,index) in facilityList" :key="index" style="display: flex">
                <div style="width: 25%">{{i.type==1?'厌氧池':i.type==2?'曝气池':i.type==3?'沉淀池':''}}</div><a-input-number v-model="i.num" /><span style="margin-left: 10px">个</span>
              </div>
<!--              <j-multi-select-tag v-model="model.facilityType" dictCode="nw_terminal_facility" type="checkbox">-->
<!--              </j-multi-select-tag>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="终端图片上传" :labelCol="rowLabelCol" :wrapperCol="rowWrapperCol" prop="pipPic">
              <j-upload type="1" bizPath="images" fileType="image"   :number="5" v-model="model.pipPic"></j-upload>
<!--              <j-upload  fileType="all" :number="4" v-model="model.pip_pic"></j-upload>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="告示牌图片上传" :labelCol="rowLabelCol" :wrapperCol="rowWrapperCol" prop="boardPic">
              <j-upload type="1" bizPath="images" fileType="image"   :number="5" v-model="model.boardPic"></j-upload>
              <!--              <j-upload  fileType="all" :number="4" v-model="model.pip_pic"></j-upload>-->
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="终端状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input-number v-model="model.status" placeholder="请输入终端状态" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="镇级站长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="townMaster">
              <a-input v-model="model.townMaster" placeholder="请输入镇级站长"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
          <a-form-model-item label="镇级站长电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="townMasterTel">
            <a-input v-model="model.townMasterTel" placeholder="请输入镇级站长电话"></a-input>
          </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="村级站长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="villageMaster">
              <a-input v-model="model.villageMaster" placeholder="请输入村级站长"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="村级站长电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="villageMasterTel">
              <a-input v-model="model.villageMasterTel" placeholder="请输入村级站长电话"></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="运维单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterpriseName">-->
<!--              <a-select-->
<!--                v-model="enVal"-->
<!--                mode="multiple"-->
<!--                placeholder="请选择运维单位"-->
<!--                style="width: 100%"-->
<!--                @change="handleChange"-->
<!--              >-->
<!--&lt;!&ndash;                <a-select-option :value="undefined">请选择</a-select-option>&ndash;&gt;-->
<!--                <a-select-option v-for="item in enterpriseList" :key="item.id" :value="item.enterpriseName">-->
<!--                  {{ item.enterpriseName }}-->
<!--                </a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="运维人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operatorId">-->
<!--              <a-select-->
<!--                v-model="opVal"-->
<!--                mode="multiple"-->
<!--                placeholder="请选择运维人员"-->
<!--                style="width: 100%"-->
<!--                @change="handleChange2"-->
<!--              >-->
<!--                &lt;!&ndash;                <a-select-option :value="undefined">请选择</a-select-option>&ndash;&gt;-->
<!--                <a-select-option v-for="item in operatorList" :key="item.id" :value="item.operatorName">-->
<!--                  {{ item.operatorName }}-->
<!--                </a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="村级监督员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="villageSupervisor">
              <a-input v-model="model.villageSupervisor" placeholder="请输入村级监督员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="村级监督员电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="villageSupervisorTel">
              <a-input v-model="model.villageSupervisorTel" placeholder="请输入村级监督员电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="目标责任" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dutyText">
              <a-input v-model="dutyText" placeholder="请输入目标责任" disabled></a-input>
            </a-form-model-item>
          </a-col>
                    <a-col :span="12">
                      <a-form-model-item label="管理部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manager_dep">
                        <a-input v-model="model.managerDep" placeholder="请输入管理部门"></a-input>
                      </a-form-model-item>
                    </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'TerminalForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      dutyText:'管网通畅、终端整洁、设施完好、运行正常',
      facilityList:[
        {
          type:1,
          num:'',
        },
        {
          type:2,
          num:'',
        },
        {
          type:3,
          num:'',
        }
      ],
      enterpriseList:[],
      operatorList:[],
      enVal:[],
      opVal:[],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      rowLabelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      rowWrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        terminalName: [{ required: true, message: '请输入终端名称!' }],
        // terminalId: [{ required: true, message: '请输入终端编号!' }],
        longitude: [{ required: true, message: '请输入经度!' }],
        latitude: [{ required: true, message: '请输入纬度!' }],
        installTime: [{ required: true, message: '请选择安装时间!' }],
      },
      url: {
        add: '/terminal/terminal/add',
        edit: '/terminal/terminal/edit',
        queryById: '/terminal/terminal/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getEnterpriseList()
    this.getOperatorIds()
  },
  methods: {
    handleChange(e){
      // console.log(e,'99999999999999')
      this.enVal= e
    },
    handleChange2(e){
      // console.log(e,'99999999999999')
      this.opVal= e
    },
    async getEnterpriseList() {
      const res = await getAction(`/ps/enterprise/list`)
      if (res.success) {
        this.enterpriseList= res.result.records
        // this.enValue=res.result.records[0].id
      }
    },
    async getOperatorIds() {
      const res = await getAction(`/ps/operator/list`)
      if (res.success) {
        this.operatorList= res.result.records
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      record.areaId = record.areaNum
      this.model = Object.assign({}, record)
      if (record.enterpriseName){
        this.enVal=record.enterpriseName.split(",");
      }
      if(record.operatorId){
        this.opVal=record.operatorId.split(",");
      }
      this.visible = true
      if(record.facilityTypeList) this.facilityList=record.facilityTypeList
    },
    submitForm() {
      // console.log(this.model)
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          const name=this.enVal.join(',')
          const name2=this.opVal.join(',')
          const installTime = this.model.installTime + ' 00:00:00'
          const params = {
            ...this.model,
            dutyText:this.dutyText,
            installTime,
            enterpriseName:name,
            operatorId:name2,
            facilityTypeList:this.facilityList
          }
          // const params={...this.model,enterpriseName:name}
          httpAction(httpurl, params, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>